package com.rockwell.robot2015;

import com.rockwell.robot2015.R;
import com.rockwell.robot2015.model.GameStage;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;

public class MainActivity extends Activity {
	GameStage m_gameStage = GameStage.Qualification;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		setupGameStageSpinner();
		setupStartButton();
		setupMaintenanceButton();
		setupTestSequenceButton();
		setupTeachButton();
		setupColorButton();
		setupDetectColorButton();
		setupSettingsButton();
		setupModbusButton();
		setupPathButton();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void setupGameStageSpinner() {
		Spinner stage = (Spinner)findViewById(R.id.spinner_stage);
		
		stage.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				switch (pos) {
				case 0:
					m_gameStage = GameStage.Qualification;
					break;
					
				case 1:
					m_gameStage = GameStage.Finals;
					break;
					
				case 2:
					m_gameStage = GameStage.TieBreaker;
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	private void setupStartButton() {
		Button start = (Button)findViewById(R.id.button_start);
		
		start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startMission();
			}
			
		});
	}
	
	private void setupMaintenanceButton() {
		Button maintenance = (Button)findViewById(R.id.button_maintenance);
		
		maintenance.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startMaintenance();
			}
			
		});
	}
	
	private void setupTestSequenceButton() {
		Button sequence = (Button)findViewById(R.id.button_sequences);
		
		sequence.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startSequences();
			}
			
		});
	}
	
	private void setupTeachButton() {
		/*
		Button teach = (Button)findViewById(R.id.button_teach);
		
		teach.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startTeach();
			}
			
		});
		*/
	}

	private void setupColorButton() {
		Button btn = (Button)findViewById(R.id.button_color);
		
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startColor();
			}
			
		});
	}

	private void setupDetectColorButton() {
		Button btn = (Button)findViewById(R.id.button_detect_color);
		
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startDetectColor();
			}
			
		});
	}

	private void startMission() {
		Intent intent = new Intent(this, RobotActivity.class);
		intent.putExtra("stage", m_gameStage.ordinal());
		
		startActivity(intent);
	}
	
	private void startMaintenance() {
		Intent intent = new Intent(this, MaintenanceActivity.class);
		intent.putExtra("stage", m_gameStage.ordinal());
		
		startActivity(intent);
	}
	
	private void startSequences() {
		Intent intent = new Intent(this, TestSequenceActivity.class);
		intent.putExtra("stage", m_gameStage.ordinal());
		
		startActivity(intent);
	}
	
	private void startTeach() {
		Intent intent = new Intent(this, TeachActivity.class);
		startActivity(intent);
	}
	
	private void startColor() {
		Intent intent = new Intent(this, ColorActivity.class);
		startActivity(intent);
	}
	
	private void startDetectColor() {
		Intent intent = new Intent(this, DetectColorActivity.class);
		startActivity(intent);
	}
	
	private void setupSettingsButton() {
		Button settings = (Button)findViewById(R.id.button_settings);
		
		settings.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				changeSettings();
			}
			
		});
	}
	
	private void changeSettings() {
		Intent intent = new Intent(this, SettingsActivity.class);
		startActivity(intent);
	}
	
	private void setupModbusButton() {
		Button settings = (Button)findViewById(R.id.button_modbus);
		
		settings.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				testModbus();
			}
			
		});
	}

	private void testModbus() {
		Intent intent = new Intent(this, ModbusActivity.class);
		startActivity(intent);
	}
	
	private void setupPathButton() {
		Button path = (Button)findViewById(R.id.button_path);
		
		path.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				definePath();
			}
			
		});
	}

	private void definePath() {
		Intent intent = new Intent(this, MazeActivity.class);
		startActivity(intent);
	}
	
}
